<template>
  <div>
    <!-- <div class="front-notice"><i class="el-icon-bell" style="margin-right: 2px"></i>公告：{{ top }}</div> -->
    <!--头部-->
    <div class="front-header">
      <div class="front-header-left">
        <router-link to="/front/home" class="logo">TC</router-link>
      </div>
      <div class="front-header-center">
        <div class="front-header-nav">
          <el-menu :default-active="$route.path" mode="horizontal" router>
            <el-menu-item index="/front/home">首页</el-menu-item>
            <el-menu-item index="/front/lostSquare">失物广场</el-menu-item>
            <el-menu-item index="/front/foundSquare">招领广场</el-menu-item>
            <el-menu-item index="/front/comment">反馈建议</el-menu-item>
          </el-menu>
        </div>
      </div>
      <div class="front-header-right">
        <div v-if="!user.username">
          <el-button @click="$router.push('/login')">登录</el-button>
          <el-button @click="$router.push('/register')">注册</el-button>
        </div>
        <div v-else>
          <el-dropdown>
            <div class="front-header-dropdown">
              <img :src="user.avatar" alt="" />
              <el-badge :is-dot="isReaded">
                <div style="margin-left: 10px">
                  <span>{{ user.name }}</span
                  ><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
                </div>
              </el-badge>
            </div>

            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div
                  style="text-decoration: none"
                  @click="navTo('/front/myLost')"
                >
                  我的失物
                </div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div
                  style="text-decoration: none"
                  @click="navTo('/front/myFound')"
                >
                  我的招领
                </div>
              </el-dropdown-item>
              <el-badge :is-dot="isReaded" class="front-header-dropdown-badge">
                <el-dropdown-item>
                  <div
                    style="text-decoration: none"
                    @click="navTo('/front/toMe')"
                  >
                    联系我的
                  </div>
                </el-dropdown-item>
              </el-badge>
              <el-dropdown-item>
                <div
                  style="text-decoration: none"
                  @click="navTo('/front/toOther')"
                >
                  我联系的
                </div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div
                  style="text-decoration: none"
                  @click="navTo('/front/person')"
                >
                  个人中心
                </div>
              </el-dropdown-item>
              <el-dropdown-item>
                <div style="text-decoration: none" @click="logout">退出</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!--主体-->
    <div class="main-body">
      <router-view ref="child" @update:user="updateUser" />
    </div>
  </div>
</template>

<script>
import { mapState,mapActions } from "vuex";
export default {
  name: "FrontLayout",

  computed: {
    ...mapState("message", {
      isReaded: (state) => state.isReaded,
    }),
  },

  data() {
    return {
      top: "",
      notice: [],
      user: JSON.parse(localStorage.getItem("xm-user") || "{}"),
    };
  },

  mounted() {
    this.loadNotice();
    this.haveToMe();
  },
  methods: {
    ...mapActions("message", {
      haveToMe: "haveToMe",
    }),
    loadNotice() {
      this.$request.get("/notice/selectAll").then((res) => {
        this.notice = res.data;
        let i = 0;
        if (this.notice && this.notice.length) {
          this.top = this.notice[0].content;
          setInterval(() => {
            this.top = this.notice[i].content;
            i++;
            if (i === this.notice.length) {
              i = 0;
            }
          }, 2500);
        }
      });
    },
    updateUser() {
      this.user = JSON.parse(localStorage.getItem("xm-user") || "{}"); // 重新获取下用户的最新信息
    },
    // 退出登录
    logout() {
      localStorage.removeItem("xm-user");
      this.$router.push("/login");
    },
    navTo(url) {
      this.$router.push(url);
    },
  },
};
</script>

<style scoped>
@import "@/assets/css/front.scss";

::v-deep .front-header-dropdown-badge .el-badge__content.is-fixed.is-dot{
  top: 8px;
  right: 12px;
}
</style>
